package com.partners.configure;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * @author liangyongle
 * @date 2019-01-16
 */
@Configuration
public class RedisConfigurtion {


    /**
     * redisTemaplate Bean的名称
     */
    public static final String REDIS_TEMPLATE_BEAN_NAME = "redisTemplate";


    /**
     * redis CacheManager
     * @return CacheManager for redis
     */
//    @Bean
//    public CacheManager redisCacheManager(LettuceConnectionFactory redisConnectionFactory) {
//        // 信息缓存配置
//        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(1);
//
//        // 给每个缓存主题做配置
//    /*    RedisCacheConfiguration userCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
//            .entryTtl(Duration.ofMinutes(15)).disableCachingNullValues().prefixKeysWith(CacheTopic.WEB_INFO);
//        redisCacheConfigurationMap.put(CacheTopic.WEB_INFO, userCacheConfiguration);*/
//
//        // 初始化一个RedisCacheWriter
//        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
//
//        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
//
//        // 设置默认过期时间是30秒
//        // defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
//
//        // 初始化RedisCacheManager
//        return new RedisCacheManager(redisCacheWriter, defaultCacheConfig, redisCacheConfigurationMap);
//
//    }


    @Bean(name = REDIS_TEMPLATE_BEAN_NAME)
    public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new JdkSerializationRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }



}
